<template>
<div>
  <el-button type="text" @click="dialogVisible = true">添加学生</el-button>
  <el-table :data="student" style="width: 100%" max-height="250">
    <el-table-column prop="id" label="序号" width="120"></el-table-column>
    <el-table-column prop="name" label="姓名" width="120"></el-table-column>
    <el-table-column prop="age" label="年龄" width="120"> </el-table-column>
    <el-table-column label="操作" width="120">
      <template slot-scope="scope">
        <el-button @click="del(scope.row.id)" type="text" size="small">移除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog
    title="请输入学生资料"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <el-form >
      <el-form-item label="ID">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="subForm">确 定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import axios from "axios"
  export default {
    methods: {
      // 添加信息
      subForm(){
        axios.post("http://127.0.0.1:3000/student",{
          student:this.form
        }).then(() => {
          console.log(this.form)
          this.getStudentList()
          this.dialogVisible = false
        })
        
      },
      // 删除信息
      del(id){
        console.log(id)
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.delete(`http://127.0.0.1:3000/student/${id}`).then(()=>{
            // this.student.splice(id,1)
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getStudentList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      // 获取信息
      getStudentList(){
        axios.get("http://127.0.0.1:3000/student").then(res => {
          this.student = res.data 
          // console.log(res.data)
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done();
          })
          .catch(() => {});
      }
    },
    data() {
      return {
        student:[],
        dialogVisible: false,
        form:{
          id:"",
          name:"",
          age:""
        }
      }
    },
    created(){
      this.getStudentList()
    },

  }
</script>